package isquirrel.network;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import isquirrel.beans.Property;
import isquirrel.beans.User;
import isquirrel.util.HibernateUtil;

import org.junit.Test;

public class ProfileComparatorTest {
	
	public User createUser1() {
		User u = new User();
		u.setFirstName("Avgoustinos");
		u.setLastName("Kadis");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}
	
	public User createUser2() {
		User u = new User();
		u.setFirstName("George");
		u.setLastName("Mixail");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}

	@Test
	public void testCompareUserProfiles() {
		
		HibernateUtil.useTestDB();
		HibernateUtil.beginTransaction();
		
		User u1 = createUser1();
		User u2 = createUser2();

		u1.addProperty(new Property("attr1","val1"));
		u1.addProperty(new Property("attr2","val2"));
		u1.addProperty(new Property("attr2","val2"));
		u1.addProperty(new Property("attr1","val1a"));
		u1.addProperty(new Property("attr2","val2"));
		u1.addProperty(new Property("attr3","val3"));
		
		u2.addProperty(new Property("attr1","val1"));
		u2.addProperty(new Property("attr1","val1"));
		u2.addProperty(new Property("attr2","val2"));
		u2.addProperty(new Property("attr2","val2"));
		u2.addProperty(new Property("attr2","val2"));
		
		HibernateUtil.commit();
		
		HibernateUtil.beginTransaction();

		double ans1 = ProfileComparator.compareUserProfiles(u1, u2);
		System.out.println(ans1);
		
		HibernateUtil.getSession().refresh(u1);
		HibernateUtil.getSession().refresh(u2);
		
		u1.addProperty(new Property("attr1","val1"));
		u1.addProperty(new Property("attr2","val2"));
		u1.addProperty(new Property("attr2","val2"));
		u1.addProperty(new Property("attr1","val1a"));
		u1.addProperty(new Property("attr2","val2"));
		u1.addProperty(new Property("attr3","val3"));
		
		u2.addProperty(new Property("attr1","val1"));
		u2.addProperty(new Property("attr1","val1"));
		u2.addProperty(new Property("attr2","val2"));
		u2.addProperty(new Property("attr2","val2"));
		u2.addProperty(new Property("attr2","val2"));
		
		HibernateUtil.commit();
		
		HibernateUtil.beginTransaction();
		
		double ans2 = ProfileComparator.compareUserProfiles(u1, u2);
		System.out.println(ans2);
		
		assertTrue(ans1 < ans2);

		//equal lists
		assertEquals(1.0,ProfileComparator.compareUserProfiles(u1, u1),0.001);
		
		//one list empty
		User u3 = createUser1(); //user without any properties
		assertEquals(0.0,ProfileComparator.compareUserProfiles(u1,u3),0.001);
		assertEquals(0.0,ProfileComparator.compareUserProfiles(u3,u1),0.001);
		
		HibernateUtil.commit();
	}

}
